import Vue from 'vue'
import Router from 'vue-router'
import Register from './views/Register.vue'
import Login from './views/Login.vue'
import Index from './views/Index.vue'
import Admin from './views/Admin.vue'
import NotFound from './views/404.vue'
import MainIndex from "./components/MainIndex.vue"
import WriteBlog from "./components/WriteBlog.vue"
import Home from "./components/Home"
import Post from "./components/Post"
import HomeBlog from "./components/Home/HomeBlog"
import HomeColl from "./components/Home/HomeColl"
import HomeFollow from "./components/Home/HomeFollow"
import AdminHome from "./components/AdminComponents/AdminHome"
import DeleteUser from "./components/AdminComponents/DeleteUser"


Vue.use(Router);
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};

const router=new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    //默认登录页面
    {
      path: '/',
      redirect:'/login'
    },
    //注册页面
    {
      path:'/register',
      name:'register',
      component:Register
    },
    //登录页面
    {
      path:'/login',
      name:'login',
      component:Login
    },
    //主页面
    {
      path:'/index',
      // name:'index',
      component:Index,
      children:[{
        path:'/',
        name:'myindex',
        component:MainIndex,//主页
      },
      {
        path:'/home/:id',
        // name:'home',
        component:Home,//个人主页
        children:[
          {
            path:'/home/:id/',
            name:'blogs',
            component:HomeBlog,
          },
          {
            path:'/home/:id/collections',
            name:'collections',
            component:HomeColl,
          },
          {
            path:'/home/:id/follows',
            name:'follows',
            component:HomeFollow,
          }
        ]
      },
      {
        path:'/writeBlog',
        name:'writeBlog',
        component:WriteBlog,//写博客
      },
      {
        path:'/post',
        name:'post',
        component:Post,
        // props:(route) => ({ post: route.query.post }),
      }
    ]
    },
    //管理员主页面
    {
      path:'/admin',
      name:'admin',
      component:Admin,
      children:[
        { path: '', component: AdminHome },
        { path: '/admin/home', name: 'home', component: AdminHome },
        { path: "/add/deleteUser",name:'deleteUser',component:DeleteUser},
      ]
    },
    //404
    {
      path:'/*',
      name:'/404',
      component:NotFound
    }
    
  ]
})

//路由守卫
router.beforeEach((to,from,next)=>{
    const isLogin = localStorage.eleToken ? true : false;
    if(to.path=="/login" || to.path=="/register"){
      next();
    }
    else{
      isLogin ? next():next("/login");
    }
})


export default router;